﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lacrima.Gobang.App
{
    /// <summary>
    /// 电脑类,计算机电脑的下棋位置
    /// </summary>
    class Computer
    {
        //电脑下的棋子是黑子还是白子
        private bool mflag;
        //电脑下棋的位置
        private int x, y;
        public Computer(bool flag)
        {
            mflag = flag;//flag为true时,电脑下黑子
        }
        //电脑类的属性:位置X
        public int M
        {
            get
            {
                return x;
            }
        }
        //电脑类的属性:位置Y
        public int N
        {
            get
            {
                return y;
            }
        }
        public void DownDot(int[,] arrchessboard)
        {
            int[,] qz = new int[15, 15];
            for (int i = 0; i < 15; i++)
            {
                for (int j = 0; j < 15; j++)
                {
                    if (arrchessboard[i, j] < 2) qz[i, j] = -1;//在该点上已经有子,权重为-1
                    else qz[i, j] = CheckQZ(i, j, arrchessboard);
                }
            }
            MaxQZ(qz);
        }
        private void MaxQZ(int[,] qz)
        {
            //计算最大的权重值.这样也得到了该点的坐标m,n
            int max = 0;
            for (int i = 0; i < 15; i++)
            {
                for (int j = 0; j < 15; j++)
                {
                    if (qz[i, j] > max)
                    {
                        x = i;
                        y = j;
                        max = qz[i, j];//这里的坐标值是外部引用属性M,N来调用
                    }
                }
            }
        }

        #region 计算指定点的权重值
        /// <summary>
        /// 计算指定点的权重值
        /// </summary>
        /// <param name="m"></param>
        /// <param name="n"></param>
        /// <param name="arrchessboard"></param>
        /// <returns></returns>
        private int CheckQZ(int m, int n, int[,] arrchessboard)
        {
            //权重加入活和死的分别,活的表示两头没有落子即为活,两头只要有一头有子即为死,h表示活,s表示死
            int qz = 0;

            //我方获胜点 连成5个
            int w1 = 100000;
            //对方获胜点,连成5个
            int w2 = 50000;

            //4个
            int w3_h = 10000;
            int w3_s = 5000;
            int w4_h = 8000;
            int w4_s = 3000;

            //3个
            int w5_h = 1000;
            int w5_s = 500;
            int w6_h = 800;
            int w6_s = 300;

            //2个
            int w7_h = 100;
            int w7_s = 50;
            int w8_h = 80;
            int w8_s = 30;

            //黑子的失败点,即禁手
            int w9 = -10000000;

            if (m == 7 && n == 7) qz += 1;

            int[] arrf = new int[4];
            if (mflag) arrchessboard[m, n] = 0;//电脑下的是黑子
            else arrchessboard[m, n] = 1; //电脑方下的是白子

            arrf[0] = Rule.Xnum(m, n, arrchessboard);
            arrf[1] = Rule.Ynum(m, n, arrchessboard);
            arrf[2] = Rule.XYnum(m, n, arrchessboard);
            arrf[3] = Rule.YXnum(m, n, arrchessboard);
            for (int i = 0; i < 4; i++)
            {
                if (Math.Abs(arrf[i]) == 5) qz += w1;
                if (arrf[i] == 4) qz += w3_h;
                if (arrf[i] == -4) qz += w3_s;
                if (arrf[i] == 3) qz += w5_h;
                if (arrf[i] == -3) qz += w5_s;
                if (arrf[i] == 2) qz += w7_h;
                if (arrf[i] == -2) qz += w7_s;
                if (mflag == true)
                {
                    if (Rule.IsFail(arrf, arrchessboard[m, n]) != 0)
                        qz += w9;//黑子在判断是否禁手
                }
            }

            //如果该点下对方的子.对方相对电脑来说是人
            if (mflag) arrchessboard[m, n] = 1;
            else arrchessboard[m, n] = 0;

            arrf[0] = Rule.Xnum(m, n, arrchessboard);
            arrf[1] = Rule.Ynum(m, n, arrchessboard);
            arrf[2] = Rule.XYnum(m, n, arrchessboard);
            arrf[3] = Rule.YXnum(m, n, arrchessboard);
            for (int i = 0; i < 4; i++)
            {
                if (Math.Abs(arrf[i]) == 5) qz += w2;
                if (arrf[i] == 4) qz += w4_h;
                if (arrf[i] == -4) qz += w4_s;
                if (arrf[i] == 3) qz += w6_h;
                if (arrf[i] == -3) qz += w6_s;
                if (arrf[i] == 2) qz += w8_h;
                if (arrf[i] == -2) qz += w8_s;
            }

            //清除该点假设的棋子
            arrchessboard[m, n] = 2;
            return qz;
        }
        #endregion
    }
}
